#!/bin/bash

Install_GitLab() {
	id -u git >/dev/null 2>&1
	[ $? -ne 0 ] && adduser -r -c 'GitLab' -m git

	#database
	mysql -uroot -p$dbrootpwd -e "CREATE DATABASE IF NOT EXISTS gitlabhq_production DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci" > /dev/null 2>&1
	mysql -uroot -p$dbrootpwd -e "GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON gitlabhq_production.* TO 'git'@'localhost' IDENTIFIED BY '123456'" > /dev/null 2>&1

	#Install go
	# curl -O --progress https://storage.googleapis.com/golang/go1.5.3.linux-amd64.tar.gz
	cd $oneinstack_dir/src
	src_url=https://storage.googleapis.com/golang/go1.5.3.linux-amd64.tar.gz && Download_src

	#切换目录
	cd /home/git
	if [ ! -d /home/git/gitlab ];then 
		sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-ce.git -b 8-7-stable gitlab
	fi
	#sudo -u git -H tar zxvf /home/git/gitlab-ce.tar.gz -C /home/git/
	#configure gitlab
	cd /home/git/gitlab
	sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml
	sudo -u git -H sed -i "s@host: localhost@host: $gitlab_sitname@" config/gitlab.yml
	sudo -u git -H sed -i 's/email_from:.*/email_from: $email_addr/' config/gitlab.yml
	sudo chown -R git log/
	sudo chown -R git tmp/
	sudo chmod -R u+rwX,go-w log/
	sudo chmod -R u+rwX tmp/
	# Make sure GitLab can write to the tmp/pids/ and tmp/sockets/ directories
	sudo chmod -R u+rwX tmp/pids/
	sudo chmod -R u+rwX tmp/sockets/
	# Create the public/uploads/ directory
	if [ ! -d public/uploads/ ]; then
		sudo -u git -H mkdir public/uploads/
	fi
	sudo chmod 0700 public/uploads
	sudo chmod -R u+rwX builds/
	sudo chmod -R u+rwX shared/artifacts/
	# Copy the example Unicorn config
	sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb
	# Copy the example Rack attack config
	sudo -u git -H cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb
	# Configure Git global settings for git user
	# 'autocrlf' is needed for the web editor
	sudo -u git -H git config --global core.autocrlf input
	# Configure Redis connection settings
	sudo -u git -H cp config/resque.yml.example config/resque.yml
	sudo -u git -H sed -i 's/^production/#production/' config/resque.yml
	sudo -u git -H sed -i '$aproduction: redis://localhost:6379' config/resque.yml
	#Configure Db
	sudo -u git -H cp config/database.yml.mysql config/database.yml
	sudo -u git -H sed -i 's@password.*@password: "$dbrootpwd"@' config/database.yml
	# Make config/database.yml readable to git only
	sudo -u git -H chmod o-rwx config/database.yml
	#Install Gems
	sudo -u git -H sed -i 's@^source.*@source "https://ruby.taobao.org/"@' Gemfile
	#过程由于网络原因等可能会连接超时问题，可以手动重试几次
	sudo -u git -H bundle install --deployment --without development test postgres aws kerberos

	su - git -c "cd /home/git/gitlab; bundle exec rake gitlab:shell:install REDIS_URL=redis://localhost:6379 RAILS_ENV=production"
	# 下面方法测试过程发现问题，安装了therubyracer，nodejs等让不起作用，应该是环境变量有关
	#sudo -u git -H bundle exec rake gitlab:shell:install REDIS_URL=redis://localhost:6379 RAILS_ENV=production
	# 安装gitlab-shell如果出现  "Gem Load Error is: Could not find a JavaScript runtime" 可尝试手动安装 therubyracer
	#sudo -u git -H /usr/local/ruby/bin/gem install --install-dir vendor/bundle/ruby/2.2.0 therubyracer  --version 0.12.2 --platform=ruby
	sudo -u git -H sed -i "s@bin.*@bin: '$redis_install_dir/bin/redis-cli'@" /home/git/gitlab-shell/config.yml

	#Install gitlab-workhorse
	cd /home/git
	sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-workhorse.git
	cd gitlab-workhorse
	sudo -u git -H git checkout v0.7.1
	#sudo -u git -H make
	su - git -c "cd /home/git/gitlab-workhorse; make"
	#Initialize Database and Activate Advanced Features
	cd /home/git/gitlab
	#sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production
	su - git -c "cd /home/git/gitlab; bundle exec rake gitlab:setup RAILS_ENV=production"

	# Install Init Script
	sudo cp lib/support/init.d/gitlab /etc/init.d/gitlab
	# Compile Assets
	#sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production
	su - git -c "cd /home/git/gitlab; bundle exec rake assets:precompile RAILS_ENV=production"
	# Start gitlab
	sudo service gitlab start

	# configure Nginx
	sudo cp lib/support/nginx/gitlab $nginx_install_dir/conf/vhost/www.gitlab.me.conf
	# server_name YOUR_SERVER_FQDN
	sed -i 's@server_name YOUR_SERVER_FQDN@server_name $gitlab_sitname@' $nginx_install_dir/conf/vhost/www.gitlab.me.conf
	#创建日志目录
	mkdir /var/log/nginx

	# resolve 502 forbidden
	sudo usermod -a -G git $run_user
	sudo chmod g+rx /home/git/
	#or chomd o+rx /home/git
	service nginx reload
	service gitlab restart

	#if mysql2 install fail
	#sudo -u git -H /usr/local/ruby/bin/gem install --install-dir vendor/bundle/ruby/2.2.0  mysql2 --version 0.3.20 --platform=ruby -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

	[ "$OS" == 'CentOS' ] && { chkconfig --add gitlab; chkconfig gitlab on; }
	cd $oneinstack_dir
}